import cefsharpAdapter from '../cefsharp'

// === 下载相关处理函数 ===
export async function handleDownloadRequests(method, url, data, params) {
    console.log('🔍 [API] 处理下载请求:', { method, url, data, params })
    
    // POST请求处理
    if (method === 'post') {
        // 串口连接
        if (/\/download\/serial\/connect$/.test(url)) {
            console.log('🔌 [API] 串口连接')
            return await cefsharpAdapter.callNativeMethod('RemoteTransConnectSerial', data)
        }
        
        // 网络连接
        if (/\/download\/network\/connect$/.test(url)) {
            console.log('🔌 [API] 网络连接')
            return await cefsharpAdapter.callNativeMethod('RemoteTransListen', data)
        }
        
        // 开始透传
        if (/\/download\/transmission\/start$/.test(url)) {
            console.log('🚀 [API] 开始透传')
            return await cefsharpAdapter.callNativeMethod('RemoteTransStart', data)
        }
        
        // 停止透传
        if (/\/download\/transmission\/stop$/.test(url)) {
            console.log('🛑 [API] 停止透传')
            return await cefsharpAdapter.callNativeMethod('RemoteTransStopTransmission', data)
        }
        
        // 更新设备信息
        if (/\/download\/device-info\/update$/.test(url)) {
            console.log('📝 [API] 更新设备信息')
            return await cefsharpAdapter.callNativeMethod('RemoteTransUpdateDeviceInfo', data)
        }
    }

    // GET请求处理
    if (method === 'get') {
        // 获取产品列表
        if (/\/download\/products$/.test(url)) {
            console.log('🔍 [API] 获取产品列表')
            return await cefsharpAdapter.callNativeMethod('RemoteTransGetProductList', params || {})
        }
        
        // 获取设备列表
        if (/\/download\/devices$/.test(url)) {
            console.log('🔍 [API] 获取设备列表')
            return await cefsharpAdapter.callNativeMethod('RemoteTransGetDevicesList', params || {})
        }
        
        // 获取透传端口
        if (/\/download\/trans-ports$/.test(url)) {
            console.log('🔍 [API] 获取透传端口')
            return await cefsharpAdapter.callNativeMethod('RemoteTransGetTransPorts', params || {})
        }

        // 获取详细设备列表
        if (/\/download\/device-detail$/.test(url)) {
            console.log('🔍 [API] 获取详细设备列表')
            return await cefsharpAdapter.callNativeMethod('RemoteTransGetDeviceDetailList', params || {})
        }

        // 获取透传状态
        if (/\/download\/transmission\/status$/.test(url)) {
            console.log('📊 [API] 获取透传状态')
            return await cefsharpAdapter.callNativeMethod('GetTransmissionStatus', params || {})
        }
        
        // 获取设备信息
        if (/\/download\/device-info$/.test(url)) {
            console.log('📱 [API] 获取设备信息')
            return await cefsharpAdapter.callNativeMethod('GetDeviceInfo', params || {})
        }
    }

    return null
}

// === 下载相关工具函数 ===
export function isDownloadEndpoint(url) {
    return /\/download/.test(url)
}

export function formatDownloadData(data) {
    // 格式化下载数据的通用函数
    return {
        timestamp: new Date().toISOString(),
        ...data
    }
}

// === 透传状态枚举 ===
export const TransmissionStatus = {
    IDLE: 'idle',
    CONNECTING: 'connecting',
    CONNECTED: 'connected',
    TRANSMITTING: 'transmitting',
    DISCONNECTED: 'disconnected',
    ERROR: 'error'
}

// === 连接类型枚举 ===
export const ConnectionType = {
    SERIAL: 'serial',
    NETWORK: 'network'
}
